package com.shbatis;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;

/**
 * @Auther: sh
 * @Date: 2025/1/29 20:47
 * @Description:
 */
public class parseShBatisXml {

    @Test
    public void parseShBatisXmlTest() throws DocumentException {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("sh-batis-config.xml"));
        Element environmentsElt = (Element)document.selectSingleNode("/configuration/environments");
        String defaultId = environmentsElt.attributeValue("default");
        System.out.println(defaultId);

        Element environmentOne = (Element)document.selectSingleNode("/configuration/environments/environment");
        System.out.println(environmentOne.attributeValue("id"));

        String managerType = environmentOne.element("transactionManager").attributeValue("type");
        System.out.println(managerType);

        String dataSourceType = environmentOne.element("dataSource").attributeValue("type");
        System.out.println(dataSourceType);

        List<Element> property = environmentOne.element("dataSource").elements();
        HashMap<String, String> propertyHashMap = new HashMap<>();
        for (Element element : property) {
            System.out.println(element);
            propertyHashMap.put(element.attributeValue("name"), element.attributeValue("value"));
        }
        System.out.println(propertyHashMap);


        // 获取sqlmapper.xml文件的路径
        Element mappersElt = (Element) document.selectSingleNode("/configuration/environments/mappers");
        mappersElt.elements().forEach(mapper -> {
            System.out.println(mapper.attributeValue("resource"));
        });
    }


    @Test
    public void testSqlMapper() throws Exception{
        // 读取xml，获取document对象
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("sqlmapper.xml"));

        // 获取namespace
        Element mapperElt = (Element) document.selectSingleNode("/mapper");
        String namespace = mapperElt.attributeValue("namespace");
        System.out.println(namespace);

        // 获取sql id
        mapperElt.elements().forEach(statementElt -> {
            // 标签名
            String name = statementElt.getName();
            System.out.println("name:" + name);
            // 如果是select标签，还要获取它的resultType
            if ("select".equals(name)) {
                String resultType = statementElt.attributeValue("resultType");
                System.out.println("resultType:" + resultType);
            }
            // sql id
            String id = statementElt.attributeValue("id");
            System.out.println("sqlId:" + id);
            // sql语句
            String sql = statementElt.getTextTrim();
            System.out.println("sql:" + sql);
        });
    }
}

